package com.tianyi.filter;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.tuckey.web.filters.urlrewrite.Conf;
import org.tuckey.web.filters.urlrewrite.UrlRewriteFilter;

import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;


/**
 * 资源重定向
 * @author cuixuhui
 */
@Component
@Order(FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER - 200)
public class MyUrlRewriteFilter extends UrlRewriteFilter {

    public static final String LOCATION_CONFIG = "classpath:/url_rewrite_cfg.xml";

    @Value(LOCATION_CONFIG)
    private Resource resource;

    @Override
    protected void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
        try {
            Conf conf = new Conf(filterConfig.getServletContext(), resource.getInputStream(), resource.getFilename(), "");
            checkConf(conf);
        } catch (IOException e) {
            throw new ServletException("加载url rewrite资源异常");
        }
    }
}
